package com.example.smart.ui.activity.home

import com.example.lib.constants.JumpConstants
import com.example.lib.extend.autoInt
import com.example.lib.extend.getIntentInt
import com.example.lib.extend.getIntentLong
import com.example.lib.extend.gridManager
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.extend.stringText
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.adapter.other.ChooseImageAdapter
import com.example.smart.base.ui.activity.BaseTitleBarActivity
import com.example.smart.databinding.ActivityUpdateSheepBinding
import com.example.smart.room.bean.AnimalBean
import com.example.smart.room.factory.RoomFactory
import com.example.smart.utils.handler.HandUtils

class UpdateSheepActivity : BaseTitleBarActivity<ActivityUpdateSheepBinding>() {
    private var ID = 0
    private val mResponse by lazy {
        RoomFactory.createAnimalRepository()
    }

    private val mImageAdapter by lazy {
        ChooseImageAdapter(this@UpdateSheepActivity)
    }

    override fun layoutID() = R.layout.activity_update_sheep

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@UpdateSheepActivity,
            statusBarColor = com.example.lib.R.color.white)
        binding.ImageRecycler.gridManager(3)
        binding.ImageRecycler.adapter = mImageAdapter
    }

    override fun initData() {
        ID = getIntentInt(JumpConstants.ID)
        mResponse.getData(ID.toLong()) { bean ->
            binding.Ear.setText(bean.earNo.toString())
            binding.Birthday.setText(bean.birthday)
            binding.Type.setText(bean.type)
            binding.Sex.setText(bean.sex)
            binding.Weight.setText(bean.weight)
            binding.Source.setText(bean.source)
            binding.DnDay.setText(bean.dnDay)
            binding.SexCount.setText(bean.sexCount)

            binding.TagImage.setImageResource(R.mipmap.icon_after_scan)
            binding.TagTitle.text = String.format(getString(R.string.scan_format), bean.type, bean.earNo.toString())

            mImageAdapter.updateList(mutableListOf(bean.img))
        }
    }

    override fun initListener() {
        setRightTextAndStyle("删除", com.example.lib.R.color.blue_326afd) {
            mResponse.delete(ID.toLong())
            ToastUtils.showToast("删除成功")
            HandUtils.postTask({
                finish()
            }, 1000)
        }

        binding.Confirm.setOnDoubleClickListener {
            val ear = binding.Ear.stringText()
            val birthday = binding.Birthday.stringText()
            val type = binding.Type.stringText()
            val sex = binding.Sex.stringText()
            val weight = binding.Weight.stringText()
            val source = binding.Source.stringText()
            val dnDay = binding.DnDay.stringText()
            val sexCount = binding.SexCount.stringText()
            if (ear.isEmpty() || birthday.isEmpty() || type.isEmpty() || sex.isEmpty() || weight.isEmpty() || source.isEmpty() || dnDay.isEmpty() || sexCount.isEmpty()) {
                ToastUtils.showToast(getString(R.string.input_non_null))
                return@setOnDoubleClickListener
            }
            mResponse.update(AnimalBean(ID.toLong(),
                ear.autoInt(),
                birthday,
                type,
                sex,
                weight,
                source,
                dnDay,
                sexCount,
                mImageAdapter.getImageList().firstOrNull()))
            binding.Confirm.isEnabled = false
            ToastUtils.showToast(getString(R.string.update_success))
            HandUtils.postTask({
                finish()
            }, 1000)
        }
    }
}